package com.zzptc.sky.baidudemo.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;

import com.zzptc.sky.baidudemo.services.IVersionUpdate;
import com.zzptc.sky.baidudemo.services.MyDownLoadService;
import com.zzptc.sky.baidudemo.utils.MyApplication;
import com.zzptc.sky.baidudemo.utils.MySharedPreferences;
import com.zzptc.sky.baidudemo.utils.MyUntils;
import com.zzptc.sky.baidudemo.vo.VersionInfo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/22.
 */

public class CheckUpdateBroadcase  extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("111","收到消息");

        SharedPreferences sp = MySharedPreferences.getSharedPreferences();
        String oldDate = sp.getString("checkDate",null);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        long q = System.currentTimeMillis();

        if (oldDate == null || oldDate.isEmpty()){
            if (date != null){
                SharedPreferences.Editor editor = sp.edit();
                String newDate  = sdf.format(date);
                editor.putString("checkDate",newDate);
                editor.commit();
            }

        }else{
            try {
                Date sdate = sdf.parse(oldDate);
                int days = MyUntils.daysBetween(sdate,date);
               //Log.d("11",String.valueOf(days));
                if (days >= 7){
                    Retrofit retrofit = new Retrofit.Builder().baseUrl("http://192.168.1.137:8080/").addConverterFactory(GsonConverterFactory.create()).build();
                    IVersionUpdate iVersionUpdate = retrofit.create(IVersionUpdate.class);
                    Call<VersionInfo> versionInfoCall = iVersionUpdate.getVersion();
                    versionInfoCall.enqueue(new Callback<VersionInfo>() {
                        @Override
                        public void onResponse(Call<VersionInfo> call, Response<VersionInfo> response) {
                            VersionInfo versionInfo = response.body();
                            if (versionInfo == null){
                                Log.d("111","versionInfo为空");
                                return;
                            }
                            int netcode = versionInfo.getVersioncode();//最新的版本号
                            int oldcode = MyUntils.getVersionCode(MyApplication.getContext());
                            if (netcode>oldcode){
                                Intent intent1 = new Intent();
                                intent1.setClass(MyApplication.getContext(), MyDownLoadService.class);
                                intent1.putExtra("apkurl",versionInfo.getApkurl());
                                MyApplication.getContext().startActivity(intent1);
                            }
                        }

                        @Override
                        public void onFailure(Call<VersionInfo> call, Throwable t) {

                        }
                    });
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }
    }
}
